package com.main.by.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.main.by.core.entity.JsonResult;
import com.main.by.entity.bo.AdminLoginBO;
import com.main.by.entity.vo.AdminLoginVO;
import com.main.by.exception.ServiceException;
import com.main.by.service.AdminService;

/**
 * 后台管理用户登录
 * @author Wendel
 * 2016年1月18日
 */
@Controller
@RequestMapping("/adminjsp/login")
public class AdminInfoController{
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	private AdminService adminService;
	
	
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String login(HttpServletRequest request){
		logger.info("系统管理员登录");
		return "adminjsp/login";
	}
	
	
	/**
	 * 系统管理员登录接口
	 * @user Wendel云
	 * 2016-1-20
	 */
	@RequestMapping(value = "loginCheck", method = RequestMethod.POST)
	@ResponseBody 
	public String loginCheck( @RequestBody AdminLoginBO admin, HttpServletRequest request, HttpServletResponse response){
		logger.info("admin login info:" + admin.toString());
		boolean flag = false;
		String errorMessage = "用户登录失败";
		JsonResult jsonResult = new JsonResult();
		try {
			admin = adminService.adminLogin(admin, request, response);
			flag = true;
		}catch (ServiceException serviceE){
			//logger.error("sys admin login failed!", serviceE);
			errorMessage = serviceE.getMessage();
		}catch (Exception e) {
			logger.error("sys admin login failed! ", e);
		}
		jsonResult.setResultCode(flag ? 0 : 1);
		jsonResult.setResultMessage(flag ? "用户登录成功" : errorMessage);
		jsonResult.setData(admin);
		return new Gson().toJson(jsonResult);
	}
	
	/**
	 * 添加管理员
	 * @user Wendel云
	 * 2016-1-26
	 */
	@RequestMapping(value = "addAdmin", method = RequestMethod.POST)
	@ResponseBody 
	public String addAdmin( @RequestBody AdminLoginVO admin, HttpServletRequest request, HttpServletResponse response){
		logger.info("admin regist info:" + admin.toString());
		boolean flag = false;
		String errorMessage = "添加管理员失败";
		JsonResult jsonResult = new JsonResult();
		try {
			flag = adminService.addAdmin(admin, request, response);
		}catch (ServiceException serviceE){
			//logger.error("admin  regist failed!", serviceE);
			errorMessage = serviceE.getMessage();
		}catch (Exception e) {
			logger.error("admin  regist failed! ", e);
		}
		jsonResult.setResultCode(flag ? 0 : 1);
		jsonResult.setResultMessage(flag ? "添加管理员成功" : errorMessage);
		jsonResult.setData(admin);
		return new Gson().toJson(jsonResult);
	}
}
